﻿namespace Rialternative.Infrastructure
{
    using System.Diagnostics.CodeAnalysis;
    using System.ServiceModel.DomainServices.Server;
    using Rialternative.DomainService;
    using Rialternative.Repositories;

    /// <summary>
    /// Rialternative bootstrapper.
    /// </summary>
    public static class Bootstrapper
    {
        /// <summary>
        /// Initializes static members of the <see cref="Bootstrapper"/> class.
        /// Initialize also the application DomainService factory.
        /// </summary>
        static Bootstrapper()
        {
            DomainService.Factory = new DomainServiceFactory();
        }

        /// <summary>
        /// Setups the IOC container.
        /// </summary>
        /// <param name="container">The ioc container.</param>
        public static void SetupContainer(IDIContainer container)
        {
            IOCProvider.Container = container;
        }

        /// <summary>
        /// Configures a specified service type to use a specified repository.
        /// </summary>
        /// <typeparam name="TDomainService">The type of the domain service.</typeparam>
        /// <typeparam name="TRepository">The type of the repository.</typeparam>
        [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "Domain service and repository types must be declared explicitly.")]
        public static void ConfigureService<TDomainService, TRepository>()
            where TDomainService : DomainService
            where TRepository : IRepository, new()
        {
            IOCProvider.Container.RegisterTypePropertyInjection(typeof(TDomainService), "Repository", typeof(TRepository));
        }
    }
}
